#!/usr/bin/env Rscript
################################
## Generalization Error
## estimation.
## K-fold, all features 
################################



# Flag to send job to cluster or 
# run in a regular computer (still in parallel,
# using MPI backend)
PegasusRun  <- commandArgs()

if(PegasusRun){
  system("bsub < GenErrorkFoldFull.job")
}else{
  #!/usr/bin/env Rscript
  
  ## Load libraries on all cores
  library(plyr)
  library(foreach)
  library(doMPI)
  
  ## Seed RNG
  set.seed(831213)
  
  ## Start cluster & worker loop
  cl  <- startMPIcluster()
  registerDoMPI(cl)
  
  ## Load libraries on master
  library(BayesTree)
  library(glmnet)
  
  
  ## Load auxiliary functions
  source("./GenErrorAuxFunc.R")
  
  ## Get processed data
  load("./FullData.RData")
  
  
  ##Get bootstrapped generalization errors
  ## Set nr. of folds
  n.folds  <- 15
  
  ## Set parameters to best paramsweep values
  bestParams <- read.csv("./BSParamSweep.csv"
                         ,nrows=1
  )[,1:6]
  bartParams  <- expand.grid(k=bestParams$k
                             ,pow=bestParams$pow
                             ,base=bestParams$base
                             ,sigdf=bestParams$sigdf
                             ,sigquant=bestParams$sigquant
                             ,ntree=bestParams$ntree
                             ,sampind=1:n.folds
                             ,burnin=50e3
                             ,niter=5e3
                             ,keepevery=10)
  bartParams  <- as.matrix(bartParams)
  
  ## Estimate generalization errors
  ## for joint model
  ## using 15-fold CV
  full.errors.boot <- err.generator(n.folds
                                    ,bartParams
                                    ,all.data$fData
                                    ,all.data$countries.full
                                    ,parallel=TRUE
                                    ,kfold=TRUE)
  
  
  save(full.errors.boot
       ,file="./GenErrorsKFoldFull.RData")
  
  closeCluster(cl)
  mpi.finalize() 
}
  